<!-- 添加教师 -->
<template>
  <div class="add">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="teacherName">
        <el-input v-model="ruleForm.teacherName"></el-input>
      </el-form-item>
      <el-form-item label="学院" prop="institute">
        <el-input v-model="ruleForm.institute"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="ruleForm.sex"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          type="password"
          v-model="ruleForm.pwd"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="cardId">
        <el-input v-model="ruleForm.cardId"></el-input>
      </el-form-item>
      <el-form-item label="职称" prop="type">
        <el-input v-model="ruleForm.type"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button type="text" @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师名字不能为空"));
      } else {
        callback();
      }
    };
    var validateSex = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师性别不能为空"));
      } else {
        callback();
      }
    };
    var validateInstitute = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师所属学院不能为空"));
      } else {
        callback();
      }
    };

    var validateTel = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师电话不能为空"));
      } else {
        callback();
      }
    };
    var validateCardId = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师身份证号不能为空"));
      } else {
        callback();
      }
    };
    var validateType = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("老师职称不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        //表单数据初始化
        teacherName: "",
        institute: "",
        sex: "",
        tel: "",
        pwd: "",
        checkPass: "",
        email: "",
        cardId: "",
        type: "",
      },
      rules: {
        teacherName: [{ validator: validateName, trigger: "blur" }],
        sex: [{ validator: validateSex, trigger: "blur" }],
        type: [{ validator: validateType, trigger: "blur" }],
        institute: [{ validator: validateInstitute, trigger: "blur" }],
        tel: [{ validator: validateTel, trigger: "blur" }],
        cardId: [{ validator: validateCardId, trigger: "blur" }],
        pwd: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios({
        url: '/api/teacher',
        method: 'post',
        data: {
          ...this.ruleForm
        }
      }).then(res => {
        if(res.data.code == 200) {
          this.$message({
            message: '数据添加成功',
            type: 'success'
          })
          this.$router.push({path: '/teacherManage'})
        }
      })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cancel() {
      //取消按钮
     this.$router.push({path: '/teacherManage'});
    },
  },
};
</script>
<style lang="scss" scoped>
.add {
  padding: 0px 40px;
  width: 400px;
}
</style>

